package com.umakr.ax.opt.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.collect.Maps;
import com.umakr.ax.common.model.Area;
import com.umakr.ax.common.service.AreaService;
import com.umakr.ax.common.vo.AreaVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author umakr
 */
@Service
public class AreaOptService {
    @Autowired
    AreaService areaService;

    public Object listAreaNodesByPid(Long pId) {
        List<AreaVO> list =  areaService.listAreaNodesByPid(pId);
        Map<String,Object> data = Maps.newLinkedHashMap();
        for (AreaVO areaVO : list){
            Long id = areaVO.getId();
            EntityWrapper<Area> areaWrapper = new EntityWrapper<>();
            areaWrapper.eq("area_pid",id);
            Integer childCount = areaService.selectCount(areaWrapper);
            //如果有子节点 则不是叶子节点
            areaVO.setLeaf(childCount > 0 ? 0:1);
        }
        int errorCode = 1 ;
        String msg = "加载成功";
        data.put("list",list);
        return areaService.resultMap(data,errorCode,msg);
    }
}
